package com.peakmain.wanandroid.mymodule.activity;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.peakmain.baselibrary.recyclerview.listener.OnItemClickListener;
import com.peakmain.baselibrary.widget.CustomPopupWindow;
import com.peakmain.minemodule.R;
import com.peakmain.minemodule.R2;
import com.peakmain.wanandroid.mymodule.adapter.PagePoupAdapter;
import com.peakmain.wanandroid.mymodule.adapter.PopupAdpater;
import com.peakmain.wanandroid.mymodule.adapter.PublicAccountAdapter;
import com.peakmain.wanandroid.mymodule.base.BaseMineActivity;
import com.peakmain.wanandroid.mymodule.contract.PublicAccountContract;
import com.peakmain.wanandroid.mymodule.presenter.PublicAccontPresenter;
import com.peakmain.wanandroid.netmodule.bean.PublicAccountBean;
import com.peakmain.wanandroid.netmodule.bean.PublicAccountListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: peakmain
 * createdata：2019/7/25
 * mail: 2726449200@qq.com
 * desiption:
 */
public class PublicAccountActivity extends BaseMineActivity<PublicAccontPresenter> implements PublicAccountContract.View {
    private CustomPopupWindow mCustomPopupWindow, mPagePopupWindow;
    private RecyclerView mPopRecyclerView, mPageRecyclerView;
    @BindView(R2.id.iv_list)
    public ImageView mIvList;
    @BindView(R2.id.recycler_view)
    public RecyclerView mRecyclerView;
    @BindView(R2.id.xrefreshview)
    public XRefreshView mXRefreshView;
    @BindView(R2.id.tv_page)
    public TextView mTvPage;

    @BindView(R2.id.tv_next_page)
    public TextView mTvNextPage;
    @BindView(R2.id.tv_pre_page)
    public TextView mTvPrePage;
    @BindView(R2.id.iv_down)
    public ImageView mIvDown;
    @BindView(R2.id.et_search)
    public EditText mEtSearch;

    public String key;
    public boolean isSearchClick = false;
    public PublicAccountAdapter mAdapter;
    private int pageNum = 1, mId, mTotalPage = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_public_account;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        mPresenter.getPublicAccountList();
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                pageNum = 1;
                mPresenter.getPublicAccountData(mId, pageNum);
            }
        });
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String key = mEtSearch.getText().toString();
                    if (!TextUtils.isEmpty(key)) {
                        pageNum = 1;
                        mPresenter.getPublicAccountSearchData(mId, pageNum, key);
                    }
                    KeyboardUtils.hideSoftInput(PublicAccountActivity.this);
                }
                return false;//返回true，保留软键盘。false，隐藏软键盘
            }
        });
    }



    @Override
    public void showPublicAccountList(final List<PublicAccountListBean> data) {
        mCustomPopupWindow = new CustomPopupWindow.PopupWindowBuilder(this)
                .setView(R.layout.popup_window_view)
                .enableBackgroundDark(true)
                .setAnimationStyle(R.style.PopupWindowAnimation)
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create();
        mPopRecyclerView = mCustomPopupWindow.getView(R.id.pop_recycler_view);
        mPopRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mPopRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        final PopupAdpater popupAdpater = new PopupAdpater(this, data);
        mCustomPopupWindow.dissmiss();
        mPopRecyclerView.setAdapter(popupAdpater);
        popupAdpater.selectPosition(0);
        mId = data.get(0).getId();
        mPresenter.getPublicAccountData(mId, pageNum);
        //mXRefreshView.startRefresh();
        popupAdpater.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                pageNum = 1;
                popupAdpater.selectPosition(position);
                mPresenter.getPublicAccountData(data.get(position).getId(), pageNum);
                mCustomPopupWindow.dissmiss();
                mId = data.get(position).getId();
            }
        });
    }

    @Override
    public void showPublicAccountDataSuccess(PublicAccountBean data) {
        setPublicAccountData(data);
    }

    private void setPublicAccountData(PublicAccountBean data) {
        mXRefreshView.stopRefresh(true);
        mAdapter = new PublicAccountAdapter(this, data.getDatas());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mAdapter);
        StringBuilder sb = new StringBuilder();
        mTotalPage = data.getPageCount();
        if (pageNum >= mTotalPage) {
            mTvNextPage.setEnabled(false);
        } else {
            mTvNextPage.setEnabled(true);
        }
        if (pageNum == 1) {
            mTvPrePage.setEnabled(false);
        } else {
            mTvPrePage.setEnabled(true);
        }
        sb.append(pageNum).append("/").append(mTotalPage);
        mTvPage.setText(sb.toString());
    }

    @Override
    public void showPublicAccountSearchSuccess(PublicAccountBean data) {
        setPublicAccountData(data);
    }

    @OnClick({R2.id.iv_list, R2.id.tv_next_page, R2.id.tv_pre_page, R2.id.ll_page, R2.id.tv_search})
    public void click(View view) {
        if (view.getId() == R.id.iv_list) {
            mCustomPopupWindow.setBackgroundDark();
            mCustomPopupWindow.showAtLocation(mIvList, Gravity.NO_GRAVITY, 0, 0);
        }
        if (view.getId() == R.id.tv_next_page) {
            pageNum++;
            if (isSearchClick && !TextUtils.isEmpty(key)) {
                mPresenter.getPublicAccountSearchData(mId, pageNum, key);
            } else {
                mPresenter.getPublicAccountData(mId, pageNum);
            }

        }
        if (view.getId() == R.id.tv_pre_page) {
            pageNum--;
            if (isSearchClick && !TextUtils.isEmpty(key)) {
                mPresenter.getPublicAccountSearchData(mId, pageNum, key);
            } else {
                mPresenter.getPublicAccountData(mId, pageNum);
            }
        }

        if (view.getId() == R.id.ll_page) {
            mPagePopupWindow = new CustomPopupWindow.PopupWindowBuilder(this)
                    .setView(R.layout.popup_window_view)
                    .enableBackgroundDark(true)
                    .setAnimationStyle(R.style.PagePopupWindowAnimation)
                    .setBgDarkAlpha(0.7f) // 控制亮度
                    .create();
            mPageRecyclerView = mPagePopupWindow.getView(R.id.pop_recycler_view);
            List<String> data = new ArrayList<>();
            for (int i = 0; i < mTotalPage; i++) {
                data.add(String.valueOf(i + 1));
            }
            final PagePoupAdapter adapter = new PagePoupAdapter(PublicAccountActivity.this, data);
            mPageRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mPageRecyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    adapter.selectPosition(position);
                    pageNum = position + 1;
                    mPresenter.getPublicAccountData(mId, pageNum);
                    mPagePopupWindow.dissmiss();
                }
            });
            mPagePopupWindow.showAtLocation(mIvDown, Gravity.TOP, 0, 0);
        }

        if (view.getId() == R.id.tv_search) {
            String key = mEtSearch.getText().toString();
            isSearchClick = true;
            if (!TextUtils.isEmpty(key)) {
                pageNum = 1;
                this.key = key;
                mPresenter.getPublicAccountSearchData(mId, pageNum, key);
            } else {
                mPresenter.getPublicAccountData(mId, pageNum);
                this.key = "";
            }
            KeyboardUtils.hideSoftInput(PublicAccountActivity.this);
        }
    }
}
